package com.wuhong.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @ClassName CrossOriginConfigBean
 * @Description TODO
 * @Author director吴
 * @Date 2021/7/30 17:21
 * @Version 1.0
 **/
@Configuration
public class CrossOriginConfigBean {

    @Bean
    public CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //corsConfiguration.addAllowedOriginPattern("*");//测试中，spring boot 2.4.4版本可以用，低版本2.2.2无此方法
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }
    @Bean
    public CorsWebFilter corsWebFilter(CorsConfiguration configuration) {

       UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());

        source.registerCorsConfiguration("/**", configuration);
        return new CorsWebFilter(source);
    }

}
